﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Telerik.Web.UI;

public partial class Topics_PopUp_SelectPost : BasePopUpPage
{
    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.ContentEditor}; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["SV"] != null)
            {
                try
                {
                    SiteVersion = DateTime.Parse(Request.QueryString["SV"]);
                }
                catch
                {
                }

            }
            if (Request.QueryString["Id"] != null)
            {
                try
                {
                    TopicId = int.Parse(Request.QueryString["Id"]);
                }
                catch
                {
                }
            }
            dsMap.SiteId = AcmsContext.SiteId;
            dsMap.CurrentSiteVersion = SiteVersion;
        }
    }

    private RadTreeNode CreateTreeNode(Post post)
    {
        RadTreeNode node = new RadTreeNode(post.DisplayName);
        node.DataItem = post;
        node.DataBind();
        node.Value = post.Id.ToString();
        if (post.GetChildren(SiteVersion).Count > 0)
        {
            node.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
        }
        return node;
    }

    DateTime? SiteVersion;
    int TopicId;

    protected override object SaveViewState()
    {
        ViewState["SiteVersion"] = SiteVersion;
        ViewState["TopicId"] = TopicId;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        SiteVersion = ViewState["SiteVersion"] as DateTime?;
        int? tmp = ViewState["TopicId"] as int?;
        if (tmp.HasValue)
            TopicId = tmp.Value;
    }

    protected void treeSite_NodeDataBound(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
    {
    }

    protected void treeSite_DataBound(object sender, EventArgs e)
    {
        if (treeSite.Nodes.Count == 1)
        {
            treeSite.Nodes[0].Expanded = true;
            foreach (RadTreeNode node in treeSite.Nodes[0].Nodes)
            {
                int id = int.Parse(node.Value);
                Post post = Post.GetById(AcmsContext.SiteId, id, SiteVersion);
                if (post != null && post.GetChildren(SiteVersion).Count > 0)
                {
                    node.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
                }
            }
        }
    }

    protected void treeSite_NodeExpand(object sender, RadTreeNodeEventArgs e)
    {
        //get node id and then create children
        int id = int.Parse(e.Node.Value);
        Post post = Post.GetById(AcmsContext.SiteId, id, SiteVersion);
        if (post != null)
        {
            foreach (Post child in post.GetChildren(SiteVersion))
            {
                e.Node.Nodes.Add(CreateTreeNode(child));
            }
        }

    }

    protected void treeSite_NodeClick(object sender, RadTreeNodeEventArgs e)
    {
        //add in the id then if there are other nodes confim if they should be added to?
        int id = int.Parse(e.Node.Value);
        PostTopic link = PostTopic.CreateInstance(id, TopicId);
        link.Update();
        if (SiteVersion != null)
        {
            //display the add for other versions
        }
        else
        {
            //close popup and update opener
        }
        ClientScript.RegisterStartupScript(this.GetType(), "Close", "returnToParent(true);", true);
    }

 
}
